<?php

namespace Modules\Search\Controllers;

use Core\Mvc\Controller;
use Core\Paginator\Adapter\Search as SearchPaginator;
use Modules\Search\Library\Search;
use Modules\Search\Models\Search as SearchModel;

class IndexController extends Controller
{

    public function indexAction()
    {
        $searchProject = $this->config->cache('searchProject');
        if ($this->request->hasPost('type')) {
            $type = $this->request->getPost('type');
            if (!isset($searchProject[$type])) {
                return $this->notFount();
            }
            if (!isset($searchProject[$type]['handle']) || $searchProject[$type]['handle'] != 'default') {
                return $this->notFount();
            }
        }
        self::$variables['#templates'] = [
            'search'
        ];
        self::$variables += [
            '#templates' => 'search',
            'title' => '搜索引擎',
            'description' => '搜索引擎',
            'keywords' => '搜索引擎'
        ];
        $searchForm = $this->form->create('search.defaultSearch');
        if ($searchForm->isValid()) {
            $searchUrl = $searchForm->save();
            if ($searchUrl) {
                return $this->temMoved($searchUrl);
            }
        }
        $hotSearch = SearchModel::find([
            'sort' => [
                'hot' => -1,
            ],
            'limit' => 30,
            'skip' => 0,
        ]);
        self::$variables['hotSearch'] = $hotSearch;
        self::$variables['data'] = $searchForm->renderForm();
    }

    public function searchAction($id, $page)
    {
        $searchModel = SearchModel::findById($id);
        if (!$searchModel) {
            return $this->notFount();
        }
        $project = $searchModel->project;
        $searchProject = $this->config->cache('searchProject');
        if (!isset($searchProject[$project]) || !isset($searchProject[$project]['defaultSearch']) || $searchProject[$project]['defaultSearch'] != true) {
            return $this->notFount();
        }
        $projectInfo = $searchProject[$project];
        if (!isset($projectInfo['limit'])) {
            $projectInfo['limit'] = 15;
        }

        if ($page == 1) {
            $searchWord = '';
            if ($this->cookies->has('searchWord')) {
                $searchWord = $this->cookies->get('searchWord');
            }
            $searchWord .= $searchModel->word;
            $this->cookies->set('searchWord', $searchWord, time() + 2592000);
        }

        $query = [
            'conditions' => '终于知道百度、网易、微博、搜狐的党委书记都是谁',
            'limit' => [$projectInfo['limit'], $projectInfo['limit'] * ($page - 1)]
        ];

        $search = new Search($project);
        $data = $search->search($query);
        $searchTotal = $search->total();
        $searchPaginator = new SearchPaginator([
            'data' => $data,
            'limit' => $projectInfo['limit'],
            'page' => $page,
            'total' => $searchTotal
        ]);

        self::$variables += [
            '#templates' => [
                'searchResult', 'searchResult-' . $project
            ],
            'title' => $searchModel->word,
            'description' => $searchModel->word . '的搜索结果',
            'word' => $searchModel->word,
            'data' => $searchPaginator->getPaginate(),
            'searchTotal' => $searchTotal,
            'search' => $search,
            'searchModel' => $searchModel
        ];
    }
}
